package app.softtech.freechat;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * 
 * @author LocDev
 *
 */
public class ActivityForgotPassword extends Activity {

	private static final String TAG = ActivityForgotPassword.class.getSimpleName();

	private EditText mEmail;
	private Button mConfirm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forgot_password);

		mEmail = (EditText) findViewById(R.id.edt_email_forgot_password);
		mConfirm = (Button) findViewById(R.id.btn_forgot_password);

		onButtonClick();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_forgot_password, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	/**
	 * 
	 * @param email
	 * @return
	 */
	private boolean confirmEmail(String email) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair(Constants.EMAIL, email));

		HttpRequest request = new HttpRequest();
		String result = request.makeServiceCall(Constants.URL_FORGOT_PASSWORD, HttpRequest.POST, params);

		try {
			JSONObject jsonObj = new JSONObject(result);
			String check = jsonObj.getString(Constants.KEY_SUCCESS);
			if (Integer.parseInt(check) == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}

	private void onButtonClick() {
		mConfirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mEmail.getText().toString().isEmpty()) {
					Log.i(TAG, "Email do not empty!");
				} else {
					new AsyncTask<Void, Void, Boolean>() {

						@Override
						protected Boolean doInBackground(Void... params) {
							if (confirmEmail(mEmail.getText().toString())) {
								return true;
							}
							return false;
						}

						@Override
						protected void onPostExecute(Boolean result) {
							if (result) {
								String urlEmail = "http://mail." + mEmail.getText().toString().substring(mEmail.getText().toString().indexOf("@") + 1);
								Intent browerIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlEmail));
								ActivityForgotPassword.this.startActivity(browerIntent);
								Log.i(TAG, "Confirm email successfully! " + "Mail server: " + urlEmail);
							} else {
								Log.i(TAG, "Confirm email failure!");
							}
						}
					}.execute();
				}
			}
		});
	}
}
